/*
 * TODO: Description
 *
 * Copyright (C) 2010 Johan Waldeback
 *  
 * This file is part of the OpenFMS project (http://www.openfms-project.org)
 * 
 * OpenFMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ModelSimulator.h"
#include <iostream>
#include <tchar.h>
using namespace std;
using namespace OpenFms;

/**
 * Checks if the specified file exists.
 * @param filename Name of file to look for.
 * @return Returns true if the specified file exist.
 */
static bool fileExists( TCHAR *filename )
{
	FILE *file = _tfopen( filename, _T("rb") );
	if( file == NULL ) {
		return false;
	}
	fclose(file);
	return true;
}


/**
 * Tests if specified simulator can load specified model
 *
 * Program exits with return code 0 if test passes, 1 if test fails.
 */
int _tmain(int argc, TCHAR *argv[])
{
	//
	// check arguments
	//
	if( argc != 4 ) {
		wcout << _T("SYNTAX: ... <simulator> <model> <PASS/FAIL>") << endl  << endl;
		wcout << _T(" <simulator> filename to model simulator engine, e.g. \"fms7aero.dll\"")<< endl;
		wcout << _T(" <model>     filename to model file, e,g, a .par file")  << endl;
		wcout << _T(" <PASS/FAIL> \"PASS\" or \"FAIL\" depending if the file should load (PASS) or not (FAIL)") << endl;
		return 1;
	}
	TCHAR *simulator = argv[1];
	TCHAR *model = argv[2];
	TCHAR *passfail = argv[3];

	//
	// should we pass or fail?
	//
	// TODO: MSVC 2008 has depricated stricmp
	bool shouldPass;
	if( _tcsicmp( _T("PASS"), passfail) == 0 ) {
		shouldPass = true;
	} 
	else if (_tcsicmp( _T("FAIL"), passfail) == 0 ) {
		shouldPass = false;
	}
	else {
		// If here,hen neither pass or fail
		// I.e. syntac error
		wcout << "ERROR: PASS/FAIL not corectly specified" << endl;
		return 1;
	}

	//
	// Print a status message
	//
	wcout << _T("Simulator   : ") << simulator << endl;
	wcout << _T("Model       : ") << model << endl;
	wcout << _T("Should load : ") << (shouldPass ? _T("YES") : _T("NO")) << endl;

	//
	// Check both files exists:
	//
	if( fileExists( simulator ) == false ) {
		wcout << _T(" ERROR: simulator file does not exist") << endl;
		return 1;
	}
	if( fileExists( model ) == false ) {
		wcout << _T(" ERROR: model file does not exist") << endl;
		return 1;
	}


	//
	// Try to load the model simulator
	//
	// TODO: make it  multi steps:
	//  1. loading sim should always succeed
	//  2. reading inpout should always succeed.
	//  3. the actual loading/paring is whats is tested with pass/fail
	ModelSimulator ms;
	bool result = ms.load( simulator, model );

	// 
	// Did it pass/fail as it should
	//
	if( result != shouldPass ){
		if( result ) {
			// If here, then load passed, but should have failed
			wcout << _T("TEST FAIL: Able to load, but shouldn't be able to") << endl;
			return 1;
		}
		else {
			// If here, then load failed, but should have passed
			wcout << _T("TEST FAIL: Unable to load, but should be able to") << endl;
			return 1;
		}
	}
	
	// if here, then test passed
	// Print an informative message and exit success.
	if( result ) {
		wcout << _T("TEST PASS: Is able to load, and should be able to load") << endl;
	}
	else {
		wcout << _T("TEST PASS: Is not able to load, and should not be able to load") << endl;
	}
	return 0;
}



